local hongru = fk.CreateSkill{
    name = "wk_heg__hongru",
    tags = { Skill.Compulsory },
}

local H = require "packages.ol_hegemony.util"

Fk:loadTranslationTable{
  ["wk_heg__hongru"] = "鸿儒",
  [":wk_heg__hongru"] = "锁定技，①你武将牌上“出牌阶段限一次”的技能发动次数+1。②结束阶段，本回合成为过你使用牌的目标的其他角色依次可选择一项：1.交给你一张牌；2.将一张牌当【杀】对你使用。",

  ["wk_heg__hongru-give"] = "交给牌",
  ["wk_heg__hongru_slash"] = "将一张牌当【杀】使用",

  ["$wk_heg__hongru1"] = "不知者，无罪。",
  ["$wk_heg__hongru2"] = "罚酒三杯，下不为例。",
}

local isPlayOneSkill = function (player, skill)
  return string.find(Fk:getDescription(skill.name, "zh_CN", player), "出牌阶段限一次，")
    and (skill:isInstanceOf(ActiveSkill) or skill:isInstanceOf(ViewAsSkill)) and H.inGeneralSkills(player, skill)
end

local RefreshHongruRecord = function (player, skill)
  local room = player.room
  local skillname = skill.name
  local usedtimes = player:getTableMark(hongru.name.."-usedtimes-phase")
  local n = player:usedEffectTimes(skillname, Player.HistoryPhase)
  if n == 0 then
    usedtimes[skillname] = 0 --for 盗书
  else
    usedtimes[skillname] = (usedtimes[skillname] or 0) + n
  end
  room:setPlayerMark(player, hongru.name.."-usedtimes-phase", usedtimes) -- 记录技能使用次数
end

local GetHongruRecord = function (player, skill)
  local skillname = skill.name
  local usedtimes = player:getTableMark(hongru.name.."-usedtimes-phase")
  return usedtimes[skillname] or 0
end

local canResetUsetimes = function (player, skill)
  if player:usedEffectTimes(skill.name, Player.HistoryPhase) > 0 then
    return GetHongruRecord(player, skill) < 2
  end
end

local ResetUseTimes = function (player, skill, islose)
  local skillname = skill.name
  local effecttimes = player:usedSkillTimes(skillname, Player.HistoryPhase) - player:usedEffectTimes(skillname, Player.HistoryPhase)
  if not islose then -- 重置的情况
    player:setSkillUseHistory(skillname, effecttimes, Player.HistoryPhase)
  else -- 失去鸿儒次数加回去的情况
    player:setSkillUseHistory(skillname, effecttimes + GetHongruRecord(player, skill), Player.HistoryPhase)
  end
end

hongru:addEffect(fk.AfterSkillEffect, {
  anim_type = "special",
  can_refresh = function(self, event, target, player, data)
    return player:hasShownSkill(hongru.name, true) and player == target and player.phase == Player.Play and isPlayOneSkill(player, data.skill)
  end,
  on_refresh = function(self, event, target, player, data)
    RefreshHongruRecord(player, data.skill)
    --print(data.skill.name..GetHongruRecord(player, data.skill)) --test
    if canResetUsetimes(player, data.skill) and player:hasShownSkill(hongru.name) then
      ResetUseTimes(player, data.skill)
    end
  end,
})

hongru:addAcquireEffect(function (self, player, is_start)
  if player.phase == Player.Play then
    local skills = player:getAllSkills()
    for _, skill in ipairs(skills) do
      if isPlayOneSkill(player, skill)  then
        RefreshHongruRecord(player, skill)
        if canResetUsetimes(player, skill) then
          ResetUseTimes(player, skill)
        end
      end
    end
  end
end)

hongru:addLoseEffect(function (self, player, is_death)
  if player.phase == Player.Play then
    local skills = player:getAllSkills()
    for _, skill in ipairs(skills) do
      if isPlayOneSkill(player, skill) then
        ResetUseTimes(player, skill, true)
      end
    end
  end
end)

hongru:addEffect(fk.EventPhaseStart,{
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(hongru.name) and target == player and player.phase == Player.Finish) then return false end
    local target_find = {}
    player.room.logic:getEventsOfScope(GameEvent.UseCard, 99, function (e)
      local use = e.data
      if use.from == player then
        for _, id in ipairs(use.tos) do
          if id ~= player then
            table.insertIfNeed(target_find, id)
          end
        end
      end
    end, Player.HistoryTurn)
    event:setCostData(self, { targets = target_find })
    return #target_find > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).targets
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
    if player.dead then break end
      if not p:isNude() then
        local choices = {"wk_heg__hongru-give"}
        local card = Fk:cloneCard("slash")
        if not p:prohibitUse(card) then
          table.insert(choices, "wk_heg__hongru_slash")
        end
        table.insert(choices, "Cancel")
        local choice = room:askToChoice(p, {choices = choices, skill_name = hongru.name})
        if choice == "wk_heg__hongru-give" then
          local cards = room:askToChooseCards(p, {
              target = p,
              min = 1,
              max = 1,
              flag = "he",
              skill_name = hongru.name,
          })
          room:obtainCard(player, cards, false, fk.ReasonGive)
        elseif choice == "wk_heg__hongru_slash" then
          local cids = room:askToCards(p,{
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = hongru.name,
            prompt = "#wk_heg__haozhu-slash:" .. player.id,
            cancelable = false,
          })
          if #cids > 0 then
            room:useVirtualCard("slash", cids, p, player, hongru.name)
          end
        end
      end
    end
  end,
})

return hongru